package com.zzy.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zzy.model.AjaxMsg;
import com.zzy.model.UserModel;
import com.zzy.service.RedisService;
import com.zzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/redis")
public class RedisController {

    private RedisService redisService;
    private String prefix = "redis/";

    @Autowired
    public RedisController(RedisService redisService) {
        this.redisService = redisService;
    }


    @RequestMapping(value="/get")
    public String get(@RequestParam(value = "ids[]") Integer[] ids){

        return prefix + "test";
    }



    @RequestMapping(value="/update")
    public String update(){

        return "redirect:getAllUser";
    }

    @RequestMapping(value="/delete")
    @ResponseBody
    public AjaxMsg delete(@RequestParam(value = "ids[]") Integer[] ids){

        return AjaxMsg.returnAjaxMsg();
    }

    @RequestMapping(value="/test")
    @ResponseBody
    public AjaxMsg test(){

        return AjaxMsg.returnAjaxMsg();
    }

    @RequestMapping(value="/val")
    @ResponseBody
    public AjaxMsg getVal(@RequestParam(value = "key") String key){
        Object  o = redisService.get(key);
        return AjaxMsg.returnAjaxMsg(o);
    }

    @RequestMapping(value="/keys")
    @ResponseBody
    public AjaxMsg getKeys(){
        // 获取数据并输出
        Set<String> keys = redisService.getKeys();
        Iterator<String> it=keys.iterator() ;
        List<String> list = new ArrayList<>();
        while(it.hasNext()){
            list.add(it.next());
        }
        return AjaxMsg.returnAjaxMsg(JSON.toJSONString(list));
    }

}